import request from '@/utils/request';
import { StateType } from '@/common-typings';
import Api from '@/services/api';

const actions: StateType = {};

Object.keys(Api).forEach(requestName => {
  actions[requestName] = async (params: StateType, options: StateType = {}): Promise<any> =>
    new Promise((resolve, reject) => {
      const newOptions = {
        method: 'POST',
        data: params,
        ...options,
      };
      request(Api[requestName], newOptions)
        .then(res => {
          // 服务器错误时无返回
          if (res) {
            resolve(res);
          }
        })
        .catch(err => {
          reject(err);
        });
    });
});

export default actions;
